package com.fh.service.impl;/**
 * 描述:
 * Author:袁亚强
 * Date:2021/11/1011:55
 */

import com.fh.entity.po.Area;
import com.fh.entity.po.User;
import com.fh.entity.vo.UserVo;
import com.fh.mappers.UserMapper;
import com.fh.service.UserService;
import com.fh.utils.JWT;
import com.fh.utils.yyqMD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * created by macro on 2021/11/10,
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    HttpServletRequest request;
    @Resource
    private UserMapper userMapper;

    @Override
    public void addUser(User user) {
        user.setCreateDate(new Date());
        String newMD5Password = yyqMD5.yyqMD5(yyqMD5.yyqMD5(user.getPassword()) + user.getName());
        user.setPassword(newMD5Password);
        userMapper.addUser(user);
    }

    @Override /*   1 登陆成功  2 账号不存在   3 密码错误     */
    public Map login(String name, String password) {
        Map map = new HashMap();
        User user = userMapper.queryByName(name);

        if (user == null) {
            map.put("code", 2);
            return map;
        }

        String s = yyqMD5.yyqMD5(yyqMD5.yyqMD5(password) + name);
        if (!user.getPassword().equals(s)) {
            map.put("code", 3);
            return map;
        }
/*
        request.getSession().setAttribute("login_user", user);
*/

        String sign = JWT.sign(user, 60 * 60 * 24);
        System.out.println(sign);
        map.put("code", 1);
        map.put("data", user);
        map.put("token",sign);
        return map;
    }


    @Override
    public List<Area> queryAreaById(Integer pid) {
        return userMapper.queryAreaById(pid);
    }

    @Override
    public Integer queryByName(String name) {
        return userMapper.queryByName(name) == null ? 0 : 1;
    }

    @Override
    public User queryById(Integer id) {
        return userMapper.queryById(id);
    }

    @Override
    public void updateUserById(User user) {
        userMapper.updateUserById(user);
    }

    @Override
    public void batchDelUser(String ids) {
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);
        userMapper.batchDelUser(list);
    }

    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);
    }

    @Override
    public List<Map<String, Object>> queryAllArea() {
        List<Area> areaList = userMapper.queryAllArea();
        return getAreaList(areaList, 0);
    }


    private List<Map<String, Object>> getAreaList(List<Area> areaList, Integer pid) {
        List<Map<String, Object>> list = new ArrayList<>();
        for (Area area : areaList) {
            Map<String, Object> map = null;
            if (pid.equals(area.getPid())) {
                map = new HashMap<>();
                map.put("id", area.getId());
                map.put("name", area.getName());
                map.put("children", getAreaList(areaList, area.getId()));
                List children = (List) map.get("children");
                if (children.size() == 0) {
                    map.remove("children");
                }
            }
            if (map != null) {
                list.add(map);
            }
        }
        return list;
    }

    private StringBuffer sb = null;

    @Override
    public String queryAllArea2() {
        List<Area> areaList = userMapper.queryAllArea();
        sb = new StringBuffer("[");
        int conut = 0;
        for (int i = 0; i < areaList.size(); i++) {
            Area area = areaList.get(i);
            if (area.getPid() != null && area.getPid() == 0) {
                conut++;
                sb.append("{");
                sb.append("\"label\":\"").append(area.getName()).append("\",\"value\":").append(area.getId());
                getChildren(area.getId(), areaList);
                sb.append("},");
            }
        }
        return sb.substring(0, sb.length() - 1) + "]";
    }

    @Override
    public Object queryUserAllData(UserVo userVo) {
        Map map = new HashMap();
        // 查询总条数
        long count = userMapper.queryCount(userVo);

        //查询分页的数据
        List<User> users = userMapper.queryUserVo(userVo);
        map.put("count", count);
        map.put("msg", "查询成功");
        map.put("data", users);
        map.put("code", 0);
        return map;
    }

    @Override
    public List<User> queryUserAll(User user) {
        return userMapper.queryUserAll(user);
    }

    @Override
    public String queryUserAreaName(User user) {
        String pName = userMapper.queryUserAreaName(user.getPid());
        String sName = userMapper.queryUserAreaName(user.getSid());
        String cName = userMapper.queryUserAreaName(user.getXid());
        return pName + sName + cName;
    }

    @Override
    public void updatePassword(User user) {
        user.setPassword(yyqMD5.yyqMD5(yyqMD5.yyqMD5(user.getPassword()) + user.getName()));
        userMapper.updatePassword(user);
    }

    @Override //1是 对   2是不对
    public Integer queryPasswordById(User user) {
        User user1 = userMapper.queryPasswordById(user);
        if (!user1.getPassword().equals(yyqMD5.yyqMD5(yyqMD5.yyqMD5(user.getPassword()) + user.getName()))) {
            return 2;
        }
        return 1;
    }

    @Override
    public void resetPassword(User user) {
        user.setPassword(yyqMD5.yyqMD5(yyqMD5.yyqMD5("123")+user.getName()));
        userMapper.updatePassword(user);
    }


    private void getChildren(Integer id, List<Area> allData) {
        sb.append("\"children\":[");
        int count = 0;
        for (int i = 0; i < allData.size(); i++) {
            Area area = allData.get(i);
            //
            if (area.getPid() != null && area.getPid().equals(id)) {
                count++;
                sb.append("{");
                sb.append("\"label\":\"").append(area.getName()).append("\",\"value\":").append(area.getId());
                getChildren(area.getId(), allData);
                sb.append("},");
            }
        }
        if (count > 0) {
            sb.replace(sb.length() - 1, sb.length(), "]");
        } else {
            sb.replace(sb.length() - 13, sb.length(), "");
        }

    }


}
